//
// Created by jwhww on 25-9-21.
//

#include "AllHeadFile.h"

CAN_RxHeaderTypeDef RxHeader_CAN1; //接收消息头结构体，一个can总线只需要初始化一个

//------------------------------------------------------------------------------------------------------
// 函数简介		   can过滤器
// 使用示例         bsp_can_filter_init(&hcan1);
// 参数介绍         *hcan                   输入你用的哪个can，对于c板来说是&hcan1 或 &hcan2
// 返回参数
// 备注信息         内部调用
//------------------------------------------------------------------------------------------------------
static void bsp_can_filter_init(CAN_HandleTypeDef *hcan)
{
    CAN_FilterTypeDef can_filter_st;
    if (hcan == &hcan1) {
        can_filter_st.FilterActivation = ENABLE;
        can_filter_st.FilterMode = CAN_FILTERMODE_IDMASK;
        can_filter_st.FilterScale = CAN_FILTERSCALE_32BIT;
        can_filter_st.FilterIdHigh = 0x0000;
        can_filter_st.FilterIdLow = 0x0000;
        can_filter_st.FilterMaskIdHigh = 0x0000;
        can_filter_st.FilterMaskIdLow = 0x0000;
        can_filter_st.FilterBank = 1;
        can_filter_st.FilterFIFOAssignment = CAN_FILTER_FIFO0;
        can_filter_st.SlaveStartFilterBank = 14;    //指定分界点。0..N-1 归 CAN1；N..27 归 CAN2。只能在配置 CAN1 时写入（写 CAN2 没效果）。

        HAL_CAN_ConfigFilter(hcan, &can_filter_st);
    }
    if (hcan == &hcan2) {
        can_filter_st.FilterActivation = ENABLE;
        can_filter_st.FilterMode = CAN_FILTERMODE_IDMASK;
        can_filter_st.FilterScale = CAN_FILTERSCALE_32BIT;
        can_filter_st.FilterIdHigh = 0x0000;
        can_filter_st.FilterIdLow = 0x0000;
        can_filter_st.FilterMaskIdHigh = 0x0000;
        can_filter_st.FilterMaskIdLow = 0x0000;
        can_filter_st.FilterBank = 14;
        can_filter_st.FilterFIFOAssignment = CAN_FILTER_FIFO1;
        can_filter_st.SlaveStartFilterBank = 14;

        HAL_CAN_ConfigFilter(hcan, &can_filter_st);
    }
}

//------------------------------------------------------------------------------------------------------
// 函数简介		  can初始化
// 使用示例         bsp_can_init(&hcan1);
// 参数介绍         *hcan                   输入你用的哪个can，对于c板来说是&hcan1 或 &hcan2
// 返回参数
// 备注信息         注意：can1使用FIFO0，can2使用FIFO1
//------------------------------------------------------------------------------------------------------
void bsp_can_init(void) {
    //can1
    bsp_can_filter_init(&hcan1);
    HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);  //FIFO0
    HAL_CAN_Start(&hcan1);
    //can2
    bsp_can_filter_init(&hcan2);
    HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);  //FIFO1
    HAL_CAN_Start(&hcan2);
}

//------------------------------------------------------------------------------------------------------
// 函数简介     CAN1-收报
// 使用示例     CAN1_Receive();
// 输入参数     hcan                    CAN句柄
// 返回参数     无
// 备注信息     无
//------------------------------------------------------------------------------------------------------
void CAN1_Receive(void) {
    uint32_t fifo = CAN_RX_FIFO0;
    uint8_t rx_data[8] = {0,0,0,0,0,0,0,0};
    // 从CAN接收数据
    if (HAL_CAN_GetRxMessage(&hcan1, fifo, &RxHeader_CAN1, rx_data) == HAL_OK) {
        // 暂时把底盘数据处理函数放在这里
        CBoard_connect_Receive(rx_data);
        // 暂时把小电脑数据处理函数放在这里
        if (RxHeader_CAN1.StdId == 0x123 || RxHeader_CAN1.StdId == 0x124) {
            small_computer_connect_Receive(rx_data);
        }
    }
}
